package com.study.spaoplog.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-06-11 09:39
 **/
@Slf4j
@Configuration
public class MyRedissonConfig {

    @Value("${redisClient}")
    String redisClient;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 所有对Redisson的使用都是通过RedissonClient
     *
     * @return
     * @throws IOException
     */
    @Bean(destroyMethod = "shutdown", name = {"redisson", "redissonClient"})
    public RedissonClient redisson() throws IOException {
        //1、创建配置
        Config config = new Config();
        config.useSingleServer().setAddress(redisClient).setKeepAlive(true);
        config.setCodec(new JsonJacksonCodec());
        //2、根据Config创建出RedissonClient实例
        //Redis url should start with redis:// or rediss://
        return Redisson.create(config);
    }

    @PostConstruct
    public void seeConfig() {
        try {
            log.info(redissonClient.getConfig().toYAML());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
